import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @author Jiange
 * @create 2021-07-28-9:14
 */
public class Study {
    static String huiyuanma = "";

    // 最多可以有十个用户注册 每个用户的用一个数组表示 第一位为acc 第二位为pass 第三位为是否登录 y/n 第四位为自己的会员卡号
    static String[][] account = new String[10][4];
    // index记录当前共有几人注册
    static int index = 0;
    static boolean loop = true;

    // 记录当前已经注册了的用户名
    static Set<String> set = new HashSet<>();

    // 记录当前的登录账户的ID
    static int current = -1;
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (loop) {
            System.out.println("*****欢迎进入奖客富翁系统******");
            System.out.println("     1.注册");
            System.out.println("     2.登录");
            System.out.println("     3.抽奖");
            System.out.println("***********************");
            System.out.print("请选择菜单：");
            try {
                int num = scanner.nextInt();
                if (num < 1 || num > 3) {
                    System.out.println("输入的序号太大 无法识别 请重新输入");
                    continue;
                }
                switch (num) {
                    case 1:
                        Register();
                        break;
                    case 2:
                        Login();
                        break;
                    case 3:
                        Lucky();
                        break;
                }

            } catch (Exception e) {
                System.out.println("输入有误，请重新输入");
                scanner = new Scanner(System.in);
            }
        }
    }

    public static void Register() {
        if (index == 10) {
            System.out.println("当前的注册人数已经够了 不能再注册了 请选择其他菜单");
            return;
        }
        System.out.println("[奖客富翁系统 > 注册]");
        System.out.println("请填写个人信息");
        System.out.print("用户名：");
        String acc = scanner.next();
        while(true){
            if(acc.matches("\\w+{5,16}@[0-9a-zA-Z]{1,5}[\\.]com")){
                break;
            }
            System.out.println("用户名必须为邮箱 重输");
            acc = scanner.next();
        }
        if (set.contains(acc)) {
            System.out.println("当前账号名已经存在请重新输入");
            return;
        }
        set.add(acc);
        System.out.println();
        System.out.print("密码：");
        String pass = scanner.next();
        while (true) {
            if(pass.length()>5){
                break;
            }
            System.out.println("密码太短 最短6位");
            pass = scanner.next();
        }
        account[index][0] = acc;
        account[index][1] = pass;
        account[index][2] = "n";

        System.out.println("注册成功，请记好您的会员卡号");
        for (int i = 0; i < 4; i++) {
            huiyuanma += Math.round(Math.random() * 9);
        }
        account[index][3] = huiyuanma;
        System.out.println("用户名     密码     会员卡号");
        System.out.println(account[index][0] + "     " + account[index][1] + "     " + account[index][3]);
        System.out.print("继续吗？（y/n）：");
        String next = scanner.next();
        if (next.equalsIgnoreCase("n")) {
            System.out.println("系统退出  谢谢！！！");
            loop = false;
        }
        huiyuanma = "";
        index++;
    }

    public static void Login() {
        boolean find = false;
        System.out.println("[奖客富翁系统 > 登录]");
        System.out.print("请输入用户名：");
        String next1 = scanner.next();
        System.out.print("请输入密码：");
        String next2 = scanner.next();
        String name = "";
        for (int i = 0; i < account.length; i++) {
            if (next1.equals(account[i][0]) && next2.equals(account[i][1])) {
                find = true;
                account[i][2] = "y";
                name = account[i][0];
                current = i;
            }
        }
        if (find) {
            System.out.println();
            System.out.println("欢迎您：" + name);

            System.out.print("继续吗？（y/n）：");
            String next3 = scanner.next();
            if (next3.equalsIgnoreCase("n")) {
                System.out.println("系统退出  谢谢！！！");
                loop = false;
            }
        } else {
            System.out.println("账号密码有误 请重新输入");
        }
    }

    public static void Lucky() {
        if (current == -1) {
            System.out.println("当前还没有人登录 请先完成登录");
            return;
        } else {
            System.out.println("[奖客富翁系统 > 抽奖]");
            System.out.println("您的卡号为" + account[current][3]);
            String[] numbers = new String[5];
            String every = "";
            boolean find = false;
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 4; j++) {
                    every += Math.round(Math.random() * 9);
                }
                for (int j2 = 0; j2 < numbers.length; j2++) {
                    if (every.equals(numbers[j2])) {
                        i--;
                        find = true;
                        break;
                    }
                }
                if (!find) {
                    numbers[i] = every;
                }
                find = false;
                every = "";
            }
            System.out.println("本日的幸运数字为：" + Arrays.toString(numbers));
            if (Arrays.asList(numbers).contains(account[current][3])) {
                System.out.println("恭喜 您" + account[current][0] + "是今日的幸运会员！");
            } else {
                System.out.println("抱歉" + account[current][0] + "您不是今日的幸运会员！");
            }
            System.out.print("继续吗？（y/n）：");
            String next3 = scanner.next();
            if (next3.equalsIgnoreCase("n")) {
                System.out.println("系统退出  谢谢！！！");
                loop = false;
            }

        }
    }
}
